/*
 * InvoiceModel.java
 * business
 *
 * Created by ChenTao on 2017/1/7.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.NormalInvoice;
import com.yidiandao.business.data.bean.TaxInvoice;
import com.yidiandao.business.data.event.RefreshNormalInvoiceEvent;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.AddNormalInvoiceRequest;
import com.yidiandao.business.data.request.ChangeInvoiceRequest;
import com.yidiandao.business.data.request.EditNormalInvoiceRequest;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.request.UpdateTaxInvoiceRequest;
import com.yidiandao.business.data.response.GetCartInvoiceResponse;
import com.yidiandao.business.data.response.QueryNormalInvoiceResponse;
import com.yidiandao.business.data.response.UpdateTaxInvoiceResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.NormalInvoiceListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 发票model
 */
public class InvoiceModel implements IInvoiceModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DisplayListItem> mNormalListItems = new ArrayList<>();

    private int normalPager;

    @Override
    public void loadNormalInvoiceList(
            final boolean isRefreshing,
            final String pagerTag,
            final OnLoadInvoiceListener listener) {

        if (isRefreshing) {
            normalPager = 1;
        }

        PagerRequest request = new PagerRequest();
        request.setUrl(Urls.NORMAL_INVOICE_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(pagerTag);
        request.setPage(normalPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryNormalInvoiceResponse.class, new NetListener<QueryNormalInvoiceResponse>() {
            @Override
            public void onSuccess(QueryNormalInvoiceResponse response) {
                normalPager++;

                if (isRefreshing) {
                    mNormalListItems.clear();
                }

                QueryNormalInvoiceResponse.Data data = response.getData();
                // 选择的默认发票的id
                int defaultInvId = data.getDefaultInvId();

                List<NormalInvoice> normalInvoices = data.getInvList();
                if (normalInvoices != null && normalInvoices.size() != 0) {
                    for (NormalInvoice invoice : normalInvoices) {
                        invoice.setSelected(invoice.getInvId().equals(defaultInvId + ""));
                        mNormalListItems.add(new NormalInvoiceListItem(invoice));
                    }
                }
                if (listener != null) {
                    listener.onSuccess(
                            mNormalListItems,
                            normalInvoices == null || normalInvoices.size() == 0,
                            response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void addNormalInvoice(String invoiceTitle, String tag, final OnNetAccessListener listener) {
        AddNormalInvoiceRequest request = new AddNormalInvoiceRequest();
        request.setUrl(Urls.ADD_NORMAL_INVOICE);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setInvTitle(invoiceTitle);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void deleteNormalInvoice(String invId, String tag, final OnNetAccessListener listener) {
        EditNormalInvoiceRequest request = new EditNormalInvoiceRequest();
        request.setUrl(Urls.DELETE_NORMAL_INVOICE);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setInvId(invId);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void updateNormalInvoice(String invId, String invTitle, String tag, final OnNetAccessListener listener) {

        EditNormalInvoiceRequest request = new EditNormalInvoiceRequest();
        request.setInvId(invId);
        request.setInvTitle(invTitle);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.UPDATE_NORMAL_INVOICE);
        request.setTag(tag);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void setDefaultNormalInvoice(String invId, String tag, final OnNetAccessListener listener) {
        EditNormalInvoiceRequest request = new EditNormalInvoiceRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setUrl(Urls.SET_DEFAULT_NORMAL_INVOICE);
        request.setInvId(invId);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void getTaxInvoice(String tag, final OnLoadTaxInvoiceListener listener) {
        BaseRequest request = new BaseRequest();
        request.setUrl(Urls.GET_TAX_INVOICE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);

        mNetBiz.sendGetRequest(request, UpdateTaxInvoiceResponse.class, new NetListener<UpdateTaxInvoiceResponse>() {
            @Override
            public void onSuccess(UpdateTaxInvoiceResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData().getTaxInvoice());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void updateTaxInvoice(TaxInvoice taxInvoice, String tag, final OnLoadTaxInvoiceListener listener) {
        UpdateTaxInvoiceRequest request = new UpdateTaxInvoiceRequest();
        request.setCompanyName(taxInvoice.getCompanyName());
        request.setTaxCode(taxInvoice.getTaxCode());
        request.setRegisterAddress(taxInvoice.getRegisterAddress());
        request.setRegisterPhone(taxInvoice.getRegisterPhone());
        request.setBankName(taxInvoice.getBankName());
        request.setBankAccount(taxInvoice.getBankAccount());
        request.setReceiveName(taxInvoice.getReceiceName());
        request.setReceiveMobile(taxInvoice.getReceiveMobile());
        request.setReceiveAddress(taxInvoice.getReceiveAddress());
        request.setUrl(Urls.UPDATE_TAX_INVOICE);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendPostRequest(request, UpdateTaxInvoiceResponse.class, new NetListener<UpdateTaxInvoiceResponse>() {
            @Override
            public void onSuccess(UpdateTaxInvoiceResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData().getTaxInvoice());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }

    @Override
    public void postRefreshNormalInvoiceEvent() {
        EventBus.getDefault().post(new RefreshNormalInvoiceEvent());
    }

    @Override
    public void getCartInvoices(String tag, final IInvoiceModel.OnGetCartInvoiceListListener listener) {

        BaseRequest request = new BaseRequest();
        request.setUrl(Urls.GET_CART_INVOICE);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, GetCartInvoiceResponse.class, new NetListener<GetCartInvoiceResponse>() {
            @Override
            public void onSuccess(GetCartInvoiceResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void changeInvoice(String tag, String invType, String invTitle, final IInvoiceModel.OnNetAccessListener listener) {

        ChangeInvoiceRequest request = new ChangeInvoiceRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.CHANGE_INVOICE);
        request.setTag(tag);
        request.setInvTitle(invTitle);
        request.setInvType(invType);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }
}
